package files.handlers;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 
 * @author Maxim
 *
 */

public class FileHandler {

	
	public static String readFileAsString(String filePath) throws java.io.IOException{
	    byte[] buffer = new byte[(int) new File(filePath).length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(filePath));
	        f.read(buffer);
	    } finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    return new String(buffer);
	}
	
	
	public static void writeToFile(String filename, String data, boolean append){
	      
		try{
			// Create file 
	    	    FileWriter fstream = new FileWriter(filename, append);
	    	    BufferedWriter out = new BufferedWriter(fstream);
	    	    out.write(data);
	    	    //Close the output stream
	    	    out.close();
	    	}
		catch (Exception e){}
	    								
	}
}
